package com.jiawa.train.controller;

import com.jiawa.train.common.resp.CommonResp;
import com.jiawa.train.req.MemberLoginReq;
import com.jiawa.train.req.MemberRegisterReq;
import com.jiawa.train.req.MemberSendCodeReq;
import com.jiawa.train.resp.MemberLoginResp;
import com.jiawa.train.service.MemberService;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/member")
public class MemberController {
	@Autowired
	private MemberService memberService;
	@GetMapping("/count")
	public CommonResp<Integer> hello() {
		int count = Math.toIntExact(memberService.count());
		CommonResp<Integer> commonResp = new CommonResp<>();
		commonResp.setContent(count);
		return commonResp;
	}
	@PostMapping("/register")
	public CommonResp<Long> register(@Valid MemberRegisterReq req) {
		long register = memberService.register(req);
//		CommonResp<Long> commonResp = new CommonResp<>();
//		 commonResp.setContent(register);
		 return new CommonResp<>(register);
	}
	@RequestMapping("/send-code")
	public CommonResp<Long> sendCode(@Valid @RequestBody MemberSendCodeReq req) {
		System.out.println("-------------------------");
		memberService.sendCode(req);
//		CommonResp<Long> commonResp = new CommonResp<>();
//		 commonResp.setContent(register);
		return new CommonResp<>();
	}
	@RequestMapping("login")
	public CommonResp<MemberLoginResp> login(@Valid @RequestBody MemberLoginReq req) {
		MemberLoginResp resp = memberService.login(req);
		return new CommonResp<>(resp);
	}
}
